﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using Xenta.Structs;

namespace Xenta.Tests
{
    [TestFixture]
    public abstract class TestSkeleton
    {
        [TestFixtureSetUp]
        public virtual void FixtureSetUp()
        {
        }

        [SetUp]
        public virtual void TestSetUp()
        {
        }

        [TearDown]
        public virtual void TestTearDown()
        {
        }

        [TestFixtureTearDown]
        public virtual void FixtureTearDown()
        {
        }
    }

    public abstract class InfrastructureTest 
        : TestSkeleton
    {
        protected ISessionFactory SessionFactory
        {
            get;
            set;
        }

        protected IInfrastructure Infrastructure
        {
            get;
            set;
        }

        protected virtual IEnumerable<Type> ModelMappings
        {
            get
            {
                return Enumerable.Empty<Type>();
            }
        }

        protected virtual IDictionary<DependencyInfo, IDependency> Dependencies
        {
            get
            {
                return null;
            }
        }

        public override void FixtureSetUp()
        {
            base.FixtureSetUp();
            var nhMapper = new ModelMapper();
            foreach(var i in ModelMappings)
                nhMapper.AddMapping(i);

            var nhConfig = new Configuration().Configure("nhibernate.xml");
            nhConfig.AddMapping(nhMapper.CompileMappingForAllExplicitlyAddedEntities());

            SessionFactory = nhConfig.BuildSessionFactory();
            var uowFactory = new UnitOfWorkFactoryMock(SessionFactory);
            Infrastructure = new InfrastructureMock(new EnvironmentMock(), 
                new UnitOfWorkPoolMock(uowFactory), 
                new EventBrokerMock(),
                new SecurityAuditorMock(),
                Dependencies);
        }
        public override void FixtureTearDown()
        {
            Infrastructure.Dispose();
            base.FixtureTearDown();
        }
    }
}
